//
//  CaiyoudaoViewController.m
//  caiyoudao
//
//  Created by yull on 16/1/25.
//  Copyright (c) 2016年 yull. All rights reserved.
//

#import "CaiyoudaoViewController.h"

@interface CaiyoudaoViewController ()

@end

@implementation CaiyoudaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = APPNAME;
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:TAB3_URL]]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString *url = [[request URL] absoluteString];
    if ([url isEqualToString:@"objc://home"]) {
        [self.tabBarController setSelectedIndex:3];
        return NO;
    } else if ([url hasPrefix:@"objc://toast/"]) {
        NSString *msg = [[url substringFromIndex:13] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self.view makeToast:msg duration:2.0f position:@"center"];
        return NO;
    }
    
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    if ([webView canGoBack]) {
        
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_top_icon3"] style:UIBarButtonItemStyleDone target:self action:@selector(webView_go_back)];
        self.navigationItem.leftBarButtonItem = backItem;
    } else {
        self.navigationItem.leftBarButtonItem = nil;
    }
}

- (void)webView_go_back {
    
    [self.webView goBack];
}

@end
